package main

import (
	"fmt"
	"sync"
	"sync/atomic"
)

var config atomic.Value

func init(){
	var initialConfig = &MyConfig{
		WhiteList: make(map[int]struct{}),
	}
	config.Store(initialConfig)
}
type MyConfig struct{
	WhiteList map[int]struct{}
}
func getConfig() *MyConfig {
	return config.Load().(*MyConfig)
}
func updateConfig1(){
	var newConfig = &MyConfig{
		WhiteList: make(map[int]struct{}),
	}

	for i := 0; i < 100; i++ {
		newConfig.WhiteList[i] = struct{}{}
	}

	config.Store(newConfig)
}

func updateConfig2() {
	var oldConfig = getConfig()
	var newConfig = &MyConfig{
		WhiteList: make(map[int]struct{}),
	}

	for k, v := range oldConfig.WhiteList {
		newConfig.WhiteList[k] = v
	}

	// add some new keys
	newConfig.WhiteList[121212] = struct{}{}
	newConfig.WhiteList[121213] = struct{}{}

	config.Store(newConfig)
}

var updateLock sync.Mutex

// update self high concurrency
func updateConfig3(){
	// lock update
	updateLock.Lock()
	defer updateLock.Unlock()

	var oldConfig = getConfig()
	var newConfig = &MyConfig{
		WhiteList: make(map[int]struct{}),
	}

	// copy from old config
	for k, v := range oldConfig.WhiteList {
		newConfig.WhiteList[k] = v
	}
	// add some new keys
	newConfig.WhiteList[121212] = struct{}{}
	newConfig.WhiteList[121213] = struct{}{}

	config.Store(newConfig)
}

func main(){
	fmt.Println("ff")
}